Start when sensor NU-M Start is active
# Set train #16 at the station
If sensor SimMode is active
    Set block New Ulm Station occupied
Endif
Assign long address 4321 as #16 in New Ulm Station
Wait for 5 seconds
Print ++++ setup done for train 16
Loop
    Print ++++ do a loop

    Wait for sensor Reserve Open to become active
    Set sensor Reserve NUM active
    Set turnout Winthrop Junction thrown
    Set turnout Mpls Depot thrown

    # Start moving
    Set function key 0 on
    Set direction to forward
    Set speed to .5

    If sensor SimMode is active
        # Proceed to Winthrop
        Set block New Ulm East occupied
        Wait for 1 second
        Set block New Ulm Station unoccupied
        Wait for 1 second
        Set block Winthrop South occupied
        Wait for 1 second
        Set block New Ulm East unoccupied
        Wait for 1 second
        Set block Winthrop Junction occupied
        Wait for 1 second
        Set block Winthrop South unoccupied
        Wait for 1 second
        Set block Winthrop Station occupied
        Wait for 1 second
        Set block Winthrop Junction unoccupied
        Wait for 1 second
    Endif

    # Station stop
    Wait for sensor Winthrop Station to become active
    Set speed to 0
    Wait for 5 seconds
    Set speed to .6

    If sensor SimMode is active
        # Move the train to Mpls
        Set block Mpls West occupied
        Wait for 1 second
        Set block Winthrop Station unoccupied
        Wait for 1 second
        Set block Mpls Depot occupied
        Wait for 1 second
        Set block Mpls West unoccupied
        Wait for 1 second
        Set block Mpls Track 2 occupied
        Wait for 1 second
        Set block Mpls Depot unoccupied
        Wait for 1 second
    Endif

    Wait for sensor Mpls Track 2 to become active
    # Release the reservation
    Set sensor Reserve Open active
    Set speed to 0
    Wait for 10 seconds

    # Get access to the junction
    Wait for sensor Reserve Open to become active
    Set sensor Reserve NUM active

    # Set the turnouts and proceed
    Set turnout Mpls Depot thrown
    Set turnout Winthrop Junction thrown
    Set direction to reverse
    Set speed to .4

    If sensor SimMode is active
        # Move the train to Winthrop
        Set block Mpls Depot occupied
        Wait for 1 second
        Set block Mpls Track 2 unoccupied
        Wait for 1 second
        Set block Mpls West occupied
        Wait for 1 second
        Set block Mpls Depot unoccupied
        Wait for 1 second
        Set block Winthrop Station occupied
        Wait for 1 second
        Set block Mpls West unoccupied
        Wait for 1 second
    Endif

    # Station stop
    Wait for sensor Winthrop Station to become active
    Set speed to 0
    Wait for 5 seconds
    Set speed to .5

    If sensor SimMode is active
        # Move train to New Ulm
        Set block Winthrop Junction occupied
        Wait for 1 second
        Set block Winthrop Station unoccupied
        Wait for 1 second
        Set block Winthrop South occupied
        Wait for 1 second
        Set block Winthrop Junction unoccupied
        Wait for 1 second
    Endif

    # Clear of shared trackage, release the reservation
    Wait for block Winthrop Junction to become unoccupied
    Set sensor Reserve Open active

    If sensor SimMode is active
        Set block New Ulm East occupied
        Wait for 1 second
        Set block Winthrop South unoccupied
        Wait for 1 second
        Set block New Ulm Station occupied
        Wait for 1 second
        Set block   New Ulm East  unoccupied
        Wait for 1 second
    Endif

    # Station stop
    Wait for sensor New Ulm Station to become active
    Set speed to 0
    Set function key 0 off
    Wait for 5 seconds
Repeat if sensor NU-M Stop is inactive

Print ++++ cleanup
If sensor SimMode is active
    Set block New Ulm Station unoccupied
Endif
Stop if sensor NU-M Stop is active
